package com.agritech.machine;

import com.agritech.environment.EnvironmentData;
import com.agritech.observer.EnvironmentObserver;

/**
 * 拖拉机观察者类
 * 实现环境观察者接口，根据环境数据调整拖拉机的工作模式
 */
public class TractorObserver implements EnvironmentObserver {
    private Tractor tractor;
    
    public TractorObserver(Tractor tractor) {
        this.tractor = tractor;
    }
    
    @Override
    public void update(EnvironmentData data) {
        System.out.println("拖拉机 " + tractor.getId() + " 收到环境数据更新:");
        
        // 根据环境数据调整拖拉机工作模式
        if (data.getSoilMoisture() > 80) {
            System.out.println("检测到土壤湿度高 (" + data.getSoilMoisture() + "%)，减少耕地深度");
            tractor.adjustPlowDepth(Math.max(5.0, tractor.getPlowDepth() - 5.0));
        } else if (data.getSoilMoisture() < 20) {
            System.out.println("检测到土壤湿度低 (" + data.getSoilMoisture() + "%)，增加耕地深度");
            tractor.adjustPlowDepth(tractor.getPlowDepth() + 5.0);
        }
        
        if (data.getWindSpeed() > 10.0) {
            System.out.println("检测到强风 (" + data.getWindSpeed() + "m/s)，降低行驶速度");
            tractor.setCurrentSpeed(Math.max(5.0, tractor.getCurrentSpeed() - 5.0));
        }
        
        if (data.getTemperature() > 35.0) {
            System.out.println("检测到高温 (" + data.getTemperature() + "°C)，注意监控发动机温度");
            // 这里可以添加发动机温度监控逻辑
        }
    }
}